package dyy.zc.alumnibook.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import dyy.zc.alumnibook.R;
import dyy.zc.alumnibook.bean.FriendCircle;
import dyy.zc.alumnibook.listener.OnItemClickListener;
import dyy.zc.alumnibook.utils.ImageLoaderUtils;
import dyy.zc.alumnibook.utils.Utils;
import dyy.zc.alumnibook.views.CircleImageView;
import dyy.zc.alumnibook.views.showimage.MyImageView;
import dyy.zc.alumnibook.views.showimage.ShowImageActivity;

/**
 * Created by chuan on 2017/4/13.
 */

public class FriendCircleAdapter extends BaseAdapter {
    int w;
    private Context context;
    private List<FriendCircle> list;

    public FriendCircleAdapter(Context context, List<FriendCircle> list, int w) {
        this.context = context;
        this.list = list;
        this.w = w;
    }

    public void setList(List<FriendCircle> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list == null || list.size() == 0){
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_friend_circle,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoaderUtils.initImage(context,list.get(position).getHead().getFileUrl(),
                holder.head,R.drawable.chat_xe_icon2_03);
        holder.name.setText(list.get(position).getName());
        holder.content.setText(list.get(position).getContent());
        holder.timeStr.setText(list.get(position).getCreatedAt());
//        Log.e("adf", list.get(position).getImg().getFileUrl() + "");
        if (list.get(position).isPic()){
            holder.img.setAddress(null, list.get(position).getImg().getFileUrl(),
                    false, dip2px(context, w - 200) + "", dip2px(context, 250) + "");
//            holder.img.setVisibility(View.VISIBLE);
//            ImageLoaderUtils.initImage(context,list.get(position).getImg().getFileUrl(),
//                    holder.img,R.drawable.chat_xe_icon2_03);
        }else {
            holder.img.setVisibility(View.GONE);
        }


        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ShowImageActivity.class);
                intent.putExtra("isGif", false);
                intent.putExtra("address", list.get(position).getImg().getFileUrl());
                intent.putExtra("width", "700");
                intent.putExtra("height", "900");
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    class ViewHolder {
        private OnItemClickListener onItemClickListener;
        private CircleImageView head;
        private TextView name,timeStr,content;
        private MyImageView img;
        public ViewHolder(View itemView) {
            head = Utils.findViewsById(itemView, R.id.head);
            name = Utils.findViewsById(itemView, R.id.name);
            timeStr = Utils.findViewsById(itemView, R.id.time);
            content = Utils.findViewsById(itemView, R.id.content);
            img = Utils.findViewsById(itemView, R.id.img);

        }
    }
}
